home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / qndxr.2 ƒ / qndxr unix main.2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-03  |  4.0 KB  |  140 lines  |  [TEXT/KAHL]

  1. /* trivial mods by ^z to prompt input, 870702,21,0906,1103 */
  2.  
  3. /*************************************************************************
  4.  *                                                                         *
  5.  *  Module:        unix main.c                                                 *
  6.  *    Programmer:    Steve Adams                                                 *
  7.  *                                                                         *
  8.  *  (C) Copyright 1985, THINK Technologies, Inc.  All rights reserved.   *
  9.  *                                                                         *
  10.  *  Alternate main program to handle Unix command lines under Lightspeed *
  11.  *  C.  The user is prompted for the command line on program entry.  The *
  12.  *  line is disected and placed in an "argv" array up to a maximum of    *
  13.  *  MAX_ARGS entries.  I/O redirection is also supported, as follows:    *
  14.  *                                                                         *
  15.  *        >         redirects stdout to following file name                     *
  16.  *        <        redirects stdin to following file name                     *
  17.  *        >>        redirects stderr to following file name                     *
  18.  *                                                                         *
  19.  *  File names following a redirection may be immediately after the      *
  20.  *  redirector, or at what appears to be the next argument.  If a file   *
  21.  *  open error occurs, then the program calls "SysBeep" and exits to the *
  22.  *  shell.                                                                 *
  23.  *                                                                         *
  24.  *  TO USE: change the "main" function in you application to "_main" and *
  25.  *  include this file in your project.                                     *
  26.  *                                                                         *
  27.  *************************************************************************/
  28.  
  29.  
  30. #include <stdio.h>
  31. #include <ctype.h>
  32. #include <proto.h>                /* LSC prototypes */
  33. #include "qndxr.2.h"            /* for my prototypes */
  34.  
  35. #define    MAX_ARGS                     50
  36.  
  37. #ifndef    true
  38. #define    true                          1
  39. #define false                          0
  40. #endif
  41.  
  42. static    int            argc            = 1;        /* final argument count  */
  43. static    char        *argv[MAX_ARGS]    = { "" };    /* array of pointers     */
  44. static    char        command[256];                /* input line buffer     */
  45. static    int            filename         = false;    /* TRUE iff file name     */
  46.  
  47.  
  48. /*************************************************************************
  49.  *                                                                         *
  50.  *  Local routine to make a "beep" and exit to the shell.                 *
  51.  *                                                                         *
  52.  *************************************************************************/
  53.  
  54. static void punt()
  55.     {
  56.     SysBeep( 5L );
  57.     ExitToShell();
  58.     }
  59.  
  60.  
  61. /*************************************************************************
  62.  *                                                                         *
  63.  *  Local routine to open a file in argv[--argc] after closing it's         *
  64.  *  previous existance.                                                     *
  65.  *                                                                         *
  66.  *************************************************************************/
  67.  
  68.  
  69. static void openfile( file, mode )
  70. char                *mode;                        /* mode for file open     */
  71. FILE                *file;                        /* file pointer to use     */
  72.     {
  73.  
  74.     if ( (file = freopen( argv[--argc], mode, file ) ) <= (FILE *) NULL)
  75.         punt();
  76.     filename = false;
  77.     }
  78.  
  79.  
  80. /*************************************************************************
  81.  *                                                                         *
  82.  *  New main routine.  Prompts for command line then calls user's main   *
  83.  * now called "_main" with the argument list and redirected I/O.         *
  84.  *                                                                         *
  85.  *************************************************************************/
  86.  
  87.  
  88. void main()
  89.     {
  90.     char            c;                            /* temp for EOLN check     */
  91.     register char    *cp;                        /* index in command line */
  92.     char            *mode;                        /* local file mode         */
  93.     FILE            *file;                        /* file to change         */
  94.     int i;
  95.     
  96.     printf ("qndxr.2 for 28-letter keys...\n");
  97.     printf("document_file_name [free_memory_size] [-e] [-a] [-s]:\n");
  98.  
  99.     gets( command );                            /* allow user to edit     */
  100.     cp = &command[0];                            /* start of buffer         */
  101.     argv[0] = "";                                /* program name is NULL  */
  102.     while (argc < MAX_ARGS)
  103.         { /* up to MAX_ARGS entries */
  104.         while (isspace( *cp++ ));
  105.         if ( !*--cp )
  106.             break;
  107.         else if ( *cp == '<' )
  108.             { /* redirect stdin */
  109.             cp++;
  110.             file = stdin;
  111.             mode = "r";
  112.             filename = true;
  113.             }
  114.         else if ( *cp == '>' )
  115.             {
  116.             mode = "w";
  117.             filename = true;
  118.             if (*++cp == '>')
  119.                 {
  120.                 file = stderr;
  121.                 cp++;
  122.                 }
  123.             else
  124.                 file = stdout;
  125.             }
  126.         else
  127.             { /* either an argument or a filename */
  128.             argv[argc++] = cp;
  129.             while ( *++cp && !isspace( *cp ) );
  130.             c = *cp;
  131.             *cp++ = '\0';
  132.             if (filename)
  133.                 openfile( file, mode );
  134.             if (!c)
  135.                 break;
  136.             }
  137.         }
  138.     _main( argc, argv );
  139.     }
  140.